import socket
import threading

##客户端


def sendmg():
    
    while True:
        try:
            sendmsg=input('输入发送内容：')
            s.sendall(bytes(sendmsg,encoding='utf-8'))
        except:
            print('与服务器连接已断开')
            return
        
def receiv():

    while True:
        try:
            acceptmsg=str(s.recv(1024),encoding='utf-8')
            print('\n'.join(acceptmsg+'\n'))
        except:
            print('与服务器连接已断开')
            return

s=None
def main():
    global s
##    host='127.0.0.1'
##    port=8800
    host=input('输入连接ip:')
    try:
        port=int(input('输入连接port:'))
    except:
        print('端口请输入整数')
        return
    s=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
    try:
        s.connect((host,port))
    except:
        print('连接失败，服务器没启动或者IP端口错误')
        return
    
    ts=threading.Thread(target=sendmg)
    tr=threading.Thread(target=receiv)
    ts.start()
    tr.start()
    ts.join()
    tr.join()

if __name__=='__main__':
    main()
